///数据库模块，用于记录一些信息。为了简便起见，先不引入这个模块。
#[macro_use]
extern crate mysql;

use std::sync::mpsc::{Sender, Receiver};
use std::thread;

pub mod operations;

pub fn db_service(sender: Sender<operations::Operations>, receiver: Receiver<operations::Operations>) {
    thread::spawn(move || {
        loop{
            let result = receiver.recv()?;
            handle_db_operate(result, &sender);
        }
    })
}

pub fn mysql_connection() -> mysql::Pool{
    let user = "root";
    let addr = "172.17.94.41";
    let port: u16 = ::std::env::var("MYSQL_SERVER_PORT").ok()
        .map(|my_port| my_port.parse::<u16>().ok().unwrap_or(3306))
        .unwrap_or(3306);
    let pwd: String = ::std::env::var("MYSQL_SERVER_PASS").unwrap_or("tima123".to_string());
    let pool = if port == 3306 && pwd == "password" {
        let pool = my::Pool::new("mysql://root:password@localhost:3306").unwrap();
        drop(pool);
        my::Pool::new_manual(1, 1, "mysql://root:password@localhost:3306").unwrap()
    } else {
        let mut builder = my::OptsBuilder::default();
        builder.user(Some(user))
            .pass(Some(pwd))
            .ip_or_hostname(Some(addr))
            .tcp_port(port);
        my::Pool::new_manual(1, 1, builder).unwrap()
    };
    pool
}

fn handle_db_operate(operate: operations::Operations, sender: &Sender<operations::Operations>){
    match operate {

    }
}